#include <iostream>
#include <cstdlib>
#include <string>
#include <unistd.h>


bool GetQuery(std::string& query){
    std::string method = getenv("METHOD");
    if(method == "GET"){
        query = getenv("QUERY");
    }
    else if(method == "POST"){
        int content_length = atoi(getenv("CONTENT_LENGTH"));
        char ch = 0;
        while(content_length){
            ssize_t s = read(0,&ch,1);
            if(s>0){
                content_length--;
                query.push_back(ch);
            }
            else if(s == 0){
                break;
            }
            else{
                return false;
            }
        }
    }
    return true;
}

void CutQuery(std::string& tar,std::string& key,std::string& value,const std::string& sep){
    size_t pos = tar.find(sep);
    if(pos != std::string::npos){
        key = tar.substr(0,pos);
        value = tar.substr(pos+sep.size());
    }
}


int main(){
    std::string query;
    GetQuery(query);

    std::string str1,str2;
    std::string name1,name2;
    std::string value1,value2;
    CutQuery(query,str1,str2,"&");
    CutQuery(str1,name1,value1,"=");
    CutQuery(str2,name2,value2,"=");

    std::cout<<name1<<":"<<value1<<std::endl;
    std::cout<<name2<<":"<<value2<<std::endl;
    return 0;
}
